/*
 *
 *
 */
#include <iostream>
#include <boost/program_options.hpp>

#include "server.h"

namespace po = boost::program_options;
using namespace std;

int main(int argc, char *argv[])
{
	po::options_description desc_visible("Visible options");
	desc_visible.add_options()
		("help,h", "Produce help message")
		("name,n", po::value<string>(), "Set map filename")
		("port,p", po::value<int>()->default_value(80), "Set port");

	po::options_description desc;
	desc.add(desc_visible);

	po::positional_options_description p;
	p.add("name", 1).add("port", 1);

    po::variables_map vm;
    try {
        po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
        po::notify(vm);
    }
    catch (const po::invalid_command_line_syntax &inv_syntax) {
        switch (inv_syntax.kind()) {
        case po::invalid_syntax::missing_parameter:
            cout << "Missing argument for option '" << inv_syntax.tokens() << "'.\n";
            break;
        default:
            cout << "Syntax error, kind " << int(inv_syntax.kind()) << "\n";
            break;
        };
        return 1;
    }
    catch (const po::unknown_option &unkn_opt) {
        cout << "Unknown option '" << unkn_opt.get_option_name() << "'\n";
        return 1;
    }

    if (vm.count("help"))
    {
    	cout << desc_visible << "\n";
		return 1;
	}

    if (!vm.count("name"))
    {
    	cout << "Map filename is not defined" << endl;
    	return 1;
    }

    string filename = vm["name"].as<string>();
	int port = vm["port"].as<int>();
	cout << "Running server for " << filename << " with port " << port << endl;

    mms::Server(filename, port).listen();

	return 0;
}
